package com.lh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lh.commonutils.R;
import com.lh.dao.ExamineMapper;
import com.lh.entity.EduTeacher;
import com.lh.entity.Examine;
import com.lh.entity.vo.ExaminQuery;
import com.lh.entity.vo.TeacherQuery;
import com.lh.dao.EduTeacherMapper;
import com.lh.feign.UcenterFeign;
import com.lh.service.IEduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author lh
 * @since 2021-06-02
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements IEduTeacherService {

    @Resource
    private ExamineMapper examineMapper;

    @Resource
    private UcenterFeign ucenterFeign;

    @Override
    public void pageQuery(Page<EduTeacher> pageParam, TeacherQuery teacherQuery) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("identity",0);
        queryWrapper.orderByAsc("sort");

        if (teacherQuery == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }

        if (!StringUtils.isEmpty(level) ) {
            queryWrapper.eq("level", level);
        }

        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }

        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }
//        排序
        queryWrapper.orderByDesc("gmt_create");

        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public Map<String, Object> getTeacherFrontList(Page<EduTeacher> pageTeacher) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        baseMapper.selectPage(pageTeacher,wrapper);
        List<EduTeacher> records = pageTeacher.getRecords();
        long current = pageTeacher.getCurrent();
        long pages = pageTeacher.getPages();
        long size = pageTeacher.getSize();
        long total = pageTeacher.getTotal();
        boolean hasNext = pageTeacher.hasNext();
        boolean hasPrevious = pageTeacher.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;

    }

    /**
     * 待审核的老师
     *
     * @param pageParam
     */
    @Override
    public void examineTeacher(Page<EduTeacher> pageParam) {
        LambdaQueryWrapper<EduTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduTeacher::getIdentity,1);
        wrapper.orderByDesc(EduTeacher::getGmtCreate);

        baseMapper.selectPage(pageParam, wrapper);

    }

    /**
     * 审核通过
     *
     * @param level
     * @param examine
     * @return
     */
    @Override
    @Transactional
    public R examinPass(Integer level, Examine examine) {

        String teacherid = examine.getTeacherid();
        EduTeacher teacher = new EduTeacher();
        teacher.setId(teacherid);
        teacher.setLevel(level);
        teacher.setIdentity(0);
        baseMapper.updateById(teacher);
        examine.setEstatus(0);
        examineMapper.insert(examine);
        ucenterFeign.changeTeacher(teacherid);
        return R.ok();
    }

    /**
     * 审核失败
     *
     * @param examine
     * @return
     */
    @Override
    public R examinFail(Examine examine) {

        String teacherid = examine.getTeacherid();
        EduTeacher teacher = new EduTeacher();
        teacher.setId(teacherid);
        baseMapper.deleteById(teacher);
        examine.setEstatus(1);
        examineMapper.insert(examine);
        return R.ok();
    }

    /**
     * 审核列表
     *
     * @return
     * @param page
     * @param limit
     * @param examinQuery
     */
    @Override
    public R examinList(Integer page, Integer limit, ExaminQuery examinQuery) {
        System.out.println(page);
        Page<Examine> pageInfo = new Page<>(page,limit);
        System.out.println(examinQuery);
        String ename =null;
        String name = null;
        if (examinQuery.getEname()!=null){
            ename=examinQuery.getEname();
        }
        if (examinQuery.getName()!=null){
            name=examinQuery.getName();
        }

        List<Examine> list= examineMapper.examinList(pageInfo,name,ename);
        Integer total=0;
        if (list.size()>0){
           total=list.size();
        }
        return R.ok().data("list",list).data("total",total);
    }

    /**
     * 根据id删除老师并且变为普通用户
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public boolean removeTeacherById(String id) {
        int i = baseMapper.deleteById(id);
        ucenterFeign.changeUcenter(id);
        return true;
    }

}
